import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/other/req_other.dart';
import 'package:flutter_changdian_football/api/req/user/req_user.dart';
import 'package:flutter_changdian_football/base/base_page_controller.dart';
import 'package:flutter_changdian_football/config/enum/refresh.dart';
import 'package:flutter_changdian_football/model/user/user_rank_model.dart';
import 'package:flutter_changdian_football/pages/home/home_controller.dart';
import 'package:flutter_changdian_football/utils/ext/refresher_extension.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class HallOfFameController extends BaseGetPageController {

  ScrollController scrollController = ScrollController();
  // 范围 0-255
  int titleAlpha = 0;

  @override
  void onInit() {
    super.onInit();
    scrollController.addListener(() {
      if (scrollController.offset > 130.w) {
        titleAlpha = 255;
      } else if (scrollController.offset <= 0) {
        titleAlpha = 0;
      } else {
        titleAlpha = scrollController.offset * 255 ~/ 130.w;
      }
      update();
    });
  }


  TextEditingController searchController = TextEditingController();


  List<UserRankModel> userRankList = [];

  @override
  void requestData(
    RefreshController controller, 
    {
      Refresh refresh = Refresh.first
    }
  ) async {
    var response = await ReqUser.userRankList(
      pageNum: page,
      keyword: searchController.text,
      pcode: HomeController.to.locationModel?.pcode
    );

    if (response.status == Status.completed) {
      if (response.data['code'] == '200') {
        /// 判断是否为最后一页, 如果是， over传true， 否则false
        if (response.data['data']['hasNextPage'] == false) {
          RefreshExtension.onSuccess(controller, refresh, true); 
        } else {
          RefreshExtension.onSuccess(controller, refresh, false);
        }

        /// 下拉刷新需要清除列表
        if (refresh != Refresh.down) {
          userRankList.clear();
          update();
        }

        for (int i = 0; i < response.data['data']['userRankInfoVOList'].length; i++) {
          userRankList.add(UserRankModel.fromJson(response.data['data']['userRankInfoVOList'][i]));
        }

        showSuccess(userRankList);
        update();
      } else if (response.data['code'] == 401) {
        RefreshExtension.onError(controller, refresh);
      } else {
        showError();  
        RefreshExtension.onError(controller, refresh);
      }
    } else {
      showError();  
      RefreshExtension.onError(controller, refresh);
    }
  }



}